//
//  ClassmateViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/8.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "ClassmateViewController.h"
#import "ClassmateTableViewCell.h"
#import "ClassmateDetailViewController.h"
#import "AddSchoolViewController.h"

@interface ClassmateViewController ()
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation ClassmateViewController
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (self.personListType == KStudentPersonListType) {
        self.title = @"我的同班同学";
    }else{
        self.title = @"我的孩子";
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem leftBarButtonItemWithTitle:@"添加孩子" target:self selector:@selector(addChildren)];
    }
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ClassmateTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"ClassmateTableViewCell"];
    [self creatDataList];
}
#pragma mark - 添加孩子
- (void)addChildren{
    AddSchoolViewController *addSchoolVC = [[AddSchoolViewController alloc]init];
    addSchoolVC.addSchoolType = @"perent";
    __weak typeof(self) unself = self;
    addSchoolVC.addSchoolBlock = ^{
        [unself creatDataList];
    };
    [self.navigationController pushViewController:addSchoolVC animated:YES];
}
- (void)creatDataList{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
//    [dic setValue:@"1" forKey:@"page"];   //页数 默认1
//    [dic setValue:KREFRESHROWS forKey:@"rows"];   //每页数量
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    NSString *urlStr = [JSPathUtil urlAppInterestGetSameClass];
    if (self.personListType == KPersentPersonListType) {
        urlStr = [JSPathUtil urlAppSoundGetChildList];
    }
    [yjNetWorkManger requestType:POST andURL:urlStr andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
//            if (self.pageNo == 1) {
                [self.dataArray removeAllObjects];
//            }
            NSArray *array = [NSArray arrayWithArray:response];
            
            for (int i = 0; i < [array count]; i++) {
                [self.dataArray addObject:[ClassmateInfo mj_objectWithKeyValues:array[i]]];
            }
//            [self refreshCount:self.dataArray.count noDataText:@"还没有同班同学" totalRow:response[@"totalRow"]];
            [self.tableView reloadData];
        }
    }];
    
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ClassmateTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ClassmateTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.classmateInfo = self.dataArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ClassmateDetailViewController *classmateDetailVC = [[ClassmateDetailViewController alloc]init];
    if (self.personListType == KPersentPersonListType) {
        classmateDetailVC.perentType = @"persent";
    }
    classmateDetailVC.classDetailInfo = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:classmateDetailVC animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectZero;
    view.backgroundColor = KBACKGROUNDCOLOR;
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
